package com.yunxi.controller.order;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yunxi.core.base.Constant;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.CurrentUser;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.order.pojo.*;
import com.yunxi.order.service.OrderVehicleDubboService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 车辆订单 前端控制器
 * </p>
 *
 * @author
 * @since 2019-06-18
 */
@RestController
@RequestMapping("/admin/ordervehicle")
@Api(tags = "pms:ordervehicle", description = "车辆订单接口")
public class OrderVehicleController {
   private static final Logger logger = LoggerFactory.getLogger(OrderVehicleController.class);

   @Autowired
   OrderVehicleDubboService orderVehicleDubboService;

   @GetMapping("/page")
   @ApiOperation(value = "查询分页")
   public ApiResult page(OrderVehicleVO orderVehicleVO, Integer pageNum, Integer pageSize) throws Exception {
      return ResponseUtil.ok(orderVehicleDubboService.findPageByVO(orderVehicleVO, pageNum, pageSize));
   }

   @GetMapping("/list")
   @ApiOperation(value = "查询列表")
   public ApiResult list(OrderVehicleVO orderVehicleVO) throws Exception {
      return ResponseUtil.ok(orderVehicleDubboService.listByVo(orderVehicleVO));
   }

   @GetMapping("/load")
   @ApiOperation(value = "读取单个")
   public ApiResult load(OrderVehicleVO orderVehicleVO, @CurrentUser LoginInfo loginInfo) throws Exception {
      return ResponseUtil.ok(orderVehicleDubboService.getById(orderVehicleVO.getOrderId()));
   }

   @PostMapping("/save")
   @ApiOperation(value = "保存信息")
   public ApiResult save(@RequestBody OrderVehicleVO orderVehicleVO, @CurrentUser LoginInfo loginInfo) throws Exception {
      orderVehicleDubboService.saveOrUpdate(orderVehicleVO);
      return ResponseUtil.ok("success");
   }

   @PostMapping("/del")
   @ApiOperation(value = "删除")
   public ApiResult del(@RequestBody List<OrderVehicleVO> orderVehicleVOList) throws Exception {
      orderVehicleDubboService.removeByListVo(orderVehicleVOList);
      return ResponseUtil.ok("success");
   }

   @GetMapping("/page2")
   @ApiOperation(value = "零售订单关联订货单分页列表")
   public ApiResult page(OrderVehicleVOReq orderVehicleVOReq, Integer pageNum, Integer pageSize) throws Exception {
      return ResponseUtil.ok(orderVehicleDubboService.findPageByVO2(orderVehicleVOReq, pageNum, pageSize));
   }

/*@PostMapping("/export")
@ApiOperation(value = "导出excel")
public Object export(@RequestParam Map<String,Object> params) throws Exception {
return ResponseUtil.ok(orderVehicleDubboService.findMapByList(params));
}*/

   //渠道商整车到货确认模块start

   @GetMapping("/listVehicleDeliveryInfo")
   @ApiOperation(value = "整车到货订单查询")
   public ApiResult listVehicleDeliveryInfo(OrderVehicleDeliveryReqVO reqVO, Integer pageNum, Integer pageSize,@CurrentUser LoginInfo user) throws Exception {

      Object partnerVO = (Object) user.getPartner();
      if(partnerVO == null){
         //
         //表示当前用户是主机厂
         return ResponseUtil.fail(401,"当前用户不是渠道用户，请使用渠道用户登录！");
      }
      reqVO.setPartnerCode(user.getPartnerCode());

      return ResponseUtil.ok(orderVehicleDubboService.listVehicleDeliveryInfo(reqVO, pageNum, pageSize));
   }


   @ApiOperation(value = "到车确认")
   @PostMapping(value = "/confirmVehicleDelivery")
   public ApiResult<Boolean> confirmVehicleDelivery(@RequestBody OrderVehicleDeliveryConfirmReqVO reqVO){
      orderVehicleDubboService.confirmVehicleDelivery(reqVO);
      return ResponseUtil.ok(true);
   }


   //整车到货确认模块end


   //整车入库  start
   @GetMapping("/listVehicleInStock")
   @ApiOperation(value = "整车入库查询")
   public ApiResult listVehicleInStock(OrderVehicleInStockReqVO reqVO, Integer pageNum, Integer pageSize,@CurrentUser LoginInfo user) throws Exception {

      Object partnerVO = (Object) user.getPartner();
      if(partnerVO == null){
         //
         //表示当前用户是主机厂
         return ResponseUtil.fail(401,"当前用户不是渠道用户，请使用渠道用户登录！");
      }
      reqVO.setPartnerCode(user.getPartnerCode());
      reqVO.setCheckStatus(Constant.PMS_ORDER_VEHICLE_CHECK_STATUS_DEALER_CONFIRM);//已签收状态 名字定义混乱
      return ResponseUtil.ok(orderVehicleDubboService.listVehicleInStock(reqVO, pageNum, pageSize));
   }


   @ApiOperation(value = "到车入库")
   @PostMapping(value = "/updateVehicleInStockStatus")
   public ApiResult<Boolean> updateVehicleInStockStatus(@RequestBody OrderVehicleInStockReqVO reqVO){
      orderVehicleDubboService.updateVehicleInStockStatus(reqVO);
      return ResponseUtil.ok(true);
   }

   @ApiOperation(value = "通过VIN模拟创建零售订单及实销数据")
   @PostMapping(value = "/createDataForExpience")
   public ApiResult<Boolean> createDataForExpience(String vin,@CurrentUser LoginInfo loginInfo){

      return ResponseUtil.ok(orderVehicleDubboService.createDataForExpience(vin));
   }
}
